package com.ambow.core.controller;


import com.ambow.core.po.DqGame;
import com.ambow.core.po.DqLabel;
import com.ambow.core.po.DqRole;
import com.ambow.core.po.GameLabel;
import com.ambow.core.service.DqGameService;
import com.ambow.core.service.DqLabelService;
import com.ambow.core.service.GameLabelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dq.gamepropaganda.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dq
 * @since 2022-09-05
 */
//跨域
@CrossOrigin
@Api(tags="游戏")
@RestController
@RequestMapping("/front/dqGame")
public class DqGameController {
    @Autowired
    private DqGameService dqGameService;
    @Autowired
    private GameLabelService gameLabelService;
    @Autowired
    private DqLabelService dqLabelService;
    //查
    @GetMapping("/list")
    @ApiOperation("查所有游戏")
    public R listAll(){
        List<DqGame> list = dqGameService.list();
        return R.ok().message("ok").data("list",list);
    }
    //id找单个游戏
    @GetMapping("/findGameById/{gameId}")
    @ApiOperation("根据id查游戏")
    public R findGameById(@PathVariable Integer gameId){
        DqGame game = dqGameService.getById(gameId);
        return R.ok().message("ok").data("game",game);
    }
    //根据名字查
    @PostMapping("/findGameListByName")
    @ApiOperation("模糊查游戏")
    public R findGameListByName(String name){
        QueryWrapper<DqGame> wrapper = new QueryWrapper<>();
        wrapper.like("game_name",name);
        List<DqGame> list = dqGameService.list(wrapper);
        return R.ok().message("ok").data("list",list);
    }
    //上传游戏
    @PostMapping("/releaseGame")
    @ApiOperation("发布游戏")
    public R releaseGame(DqGame game){
        boolean save = dqGameService.save(game);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据游戏查标签
    @GetMapping("/findLabelByGameId/{gameId}")
    @ApiOperation("根据游戏找标签")
    //多对多
    public R findLabelByGameId(@PathVariable Integer gameId){
        QueryWrapper<GameLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("game_id",gameId);
        List<GameLabel> lists = gameLabelService.list(wrapper);
        List<DqLabel> labels = new ArrayList<>();
        for (GameLabel list : lists) {
            labels.add(dqLabelService.getById(list.getLabelId()));
        }
        System.out.println(labels);
        return R.ok().data("label",labels);
    }
    //根据标签查游戏
    @GetMapping("/findGamelByLabelId/{labelId}")
    @ApiOperation("根据标签找游戏")
    //多对多
    public R findGamelByLabelId(@PathVariable Integer labelId){
        QueryWrapper<GameLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("label_id",labelId);
        List<GameLabel> lists = gameLabelService.list(wrapper);
        ArrayList<DqGame> games = new ArrayList<>();
        for (GameLabel list : lists) {
            games.add(dqGameService.getById(list.getGameId()));
        }
        System.out.println(games);
        return R.ok().data("games",games);
    }



}

